﻿using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System.Threading.Tasks;

namespace DataTransformationTasks.BaseDriver;

public class Strategy
{
    protected ILogger Logger { get; set; } = NullLogger.Instance;
    protected int SweepMs { get; }

    protected Strategy(Machine machine)
    {
        Logger = LoggerFactory.CreateLogger(GetType().FullName);
        Machine = machine;
        SweepMs = machine.Configuration.type["sweep_ms"];
    }

    public Machine Machine { get; }
    public bool LastSuccess { get; protected set; }
    public bool IsHealthy { get; protected set; }

    public virtual Task<dynamic> CreateAsync()
    {
        return Task.FromResult<dynamic>(null);
    }

    public virtual Task<dynamic> InitializeAsync()
    {
        return Task.FromResult<dynamic>(null);
    }

    protected virtual Task<dynamic> CollectAsync()
    {
        return Task.FromResult<dynamic>(null);
    }

    public virtual async Task SweepAsync(int delayMs = -1)
    {
        delayMs = delayMs < 0 ? SweepMs : delayMs;
        await Task.Delay(delayMs);
        LastSuccess = false;
        await CollectAsync();
        await Machine.Handler.OnStrategySweepCompleteInternalAsync();
    }
}